#!/usr/bin/env python2
# ----------------------------------------------------------------------------
# "The ihave script" - this advertises what posts a site knows about so that
# others get a lead for synchronizing. I expect this gets run once a day for
# small networks with the default MAXAGE, and perhaps every 3-4 hours with
# MAXAGE=2
# ----------------------------------------------------------------------------
# Copyright 2021 Chris Baird, VK2CJB, chris.j.baird@gmail.com Usage and
# distribution as per the GPL3.0
#
# But let's not give this to just anyone who has zero idea about -correct-
# Netnews posting standards. The members of OzAPRS know where I live...
# ----------------------------------------------------------------------------
# 20210326: Changing over from using S: to A:
#
# "newsspool" - don't announce incomplete messages
# "incomplete" - only announce incomplete messages
# <integer> = maximum age of files to consider
# default: announce everything that's less than 30 days old.

import os, time, zlib, sys, re
import site_config as cfg
import qrpbbb_lib as lib

MAXAGE = 32
cnthave = 0
cntpackets = 0
donewsspool = 1
doincomplete = 1

print "\nqrpbbb_ihave.py executing at:", time.ctime()

while len(sys.argv) > 1:
   if sys.argv[1] == "newsspool":
      doincomplete = 0
   if sys.argv[1] == "incomplete":
      donewsspool = 0
   if sys.argv[1].isdigit():
      MAXAGE = int(sys.argv[1])
      print "Examining messages created less than",MAXAGE,"days ago."
   sys.argv = sys.argv[1:]

#
#
#

now = time.time()
have = []
p = 0

if donewsspool:
   print "\nIndexing messages in the newsspool..."

   for f in lib.newsspool_filelist():
      if (now - os.path.getmtime (f)) / 86400 < MAXAGE:
         all = lib.fetch_messagetext(f)

         # distributions other than the 'default' qrpbbb are mentioned
         # if the dist is not compatible with the radiomode, ignore
         msgid = lib.message_hash(all)
         lines = all.splitlines()
         distro = filter(lambda v: re.match("^Distribution:", v), lines)[0].rstrip()
         distro = distro.split()[1]
         if distro in cfg.ACCEPTDISTRO:
            if distro == "qrpbbb":
               have.append ((msgid, str(len(lines))))
            else:
               have.append ((msgid, str(len(lines)), distro))
            cnthave += 1

   print "Messages in newsspool:", len(have)
   p = len(have)

# Now find the msgids from partially retrieved posts, which other stations
# might be in a better position to recover.
# Putting the maxage filter on these isn't terribly useful, but.. eh
# (perhaps an option to messages older than a limit?)

if doincomplete:
   print "Indexing messages in the incoming queue..."

   for f in lib.queuein_filelist():
      if (now - os.path.getmtime (f)) / 86400 < MAXAGE:
         msgid = os.path.basename(f)
         all = lib.fetch_messagetext(f)

         # find the last non-blank line, if we can..
         lines = all.splitlines()
         have.append ((msgid, str(len(lines))))
         cnthave += 1

   print "Messages in queue_in:",len(have)-p, "\n"

#
#
#

while len(have) > 0:
      s = cfg.APRSPREAM + "A"
      while len(have) > 0 and len(s) < 128:
         s += ":" + ','.join(have[0])
         have = have[1:]
      lib.packetout(s, priority=190)
      cntpackets += 1

print "\nMessages known:", cnthave
if (cntpackets):
   print "Packets generated:", cntpackets

#
#
#
